<template>
  <div>
    <Card class="search-condition">
      <Row>
        <span>计算规则：</span>
        <dict code="JSGZ" model="pageModel.ruleId" :value="pageModel.ruleId" @click="getVal" @data="getData" style="width:280px"></dict>
        <Button @click="fetchListStart" class="m-l-sm" type="primary" icon="md-search">筛选</Button>
        <Button @click="reset" class="m-l-sm" icon="md-sync">重置</Button>
      </Row>
    </Card>
    <Row class="m-b-sm m-t-sm">
      <Button @click="add" type="primary" icon="md-add">新增定时</Button>
    </Row>
    <Card>
      <Row>
        <Table ref="table" :loading="loading" :height="autoHeight" stripe border :columns="tableColumn" :data="tableData"></Table>
        <page-temp :total="total" :current="page.pageNo" :size="page.pageSize" @changePage="changePage"></page-temp>
        <task-modal :visible.sync="visible" :rules="rules" :model-temp="scheduledModel" @fetch="fetchListStart"></task-modal>
      </Row>
    </Card>
  </div>
</template>
<script>
import dict from "_c/dict/dict.vue";
import dictSpan from "_c/dict/dict-span.vue";
import taskModal from "_c/calculation/task-modal.vue";
import _ from "lodash";
import Api from "@/api/calculation";
import autoHeight from '@/mixins/auto-height'
const api = new Api();
export default {
  components: {
    dict,
    dictSpan,
    taskModal
  }, 
  mixins:[autoHeight],
  data() {
    return {
      loading: true, 
      rules: [],
      total: 0,
      visible: false,
      scheduledModel: {},
      // 暂无分页
      page: {
        // pageNo: 1,
        // pageSize: this.$config.PAGE_SIZE
      },
      pageModel: {},
      tableColumn: [
        {
          title: "序号",
          width: 80,
          render: (h, params) => {
            return h(
              "span",
              params.index + 1
              // this.page.pageSize * (this.pageNo - 1) + params.index + 1
            );
          }
        },
        {
          title: "规则名称",
          key: "ruleId",
          render: (h, params) => {
            let self = this;
            let title = (
              self.rules.find(x => x.ruleId === params.row.ruleId) || {
                ruleName: params.row.ruleId
              }
            ).ruleName;
            return h("span", title);
          }
        },
        {
          title: "计算周期",
          key: "weekNum",
          render: (h, params) => {
            return h(dictSpan, {
              props: {
                code: "ZGX_WEEK",
                value: params.row.weekNum
              }
            });
          }
        },
        {
          title: "计算时间",
          key: "time"
        },
        // {
        //   title: "状态",
        //   key: "status",
        //   render: (h, params) => {
        //     return h(dictSpan, {
        //       props: {
        //         code: "status",
        //         value: params.row.status
        //       }
        //     });
        //   }
        // },
        {
          title: "操作",
          render: (h, params) => {
            let statusStr = params.row.status === "1" ? "停用" : "启用";
            let self = this;
            return h("div", [
              h(
                "Button",
                {
                  style: {
                    marginRight: "5px"
                  },
                  props: {
                    type: "primary",
                    size: "small"
                  },
                  on: {
                    click: () => {
                      self.scheduledModel = _.pick(params.row, [
                        "scheduledId",
                        "ruleId",
                        "weekNum",
                        "time"
                      ]);
                      self.visible = true;
                    }
                  }
                },
                "编辑"
              ),
              h(
                "Poptip",
                {
                  props: {
                    confirm: true,
                    title: `您确定要${statusStr}这条规则吗?`,
                    transfer: true,
                    placement: "top"
                  },
                  on: {
                    "on-ok": () => {
                      self.updateStatus(
                        params.row.scheduledId,
                        params.row.status
                      );
                    }
                  }
                },
                [
                  h(
                    "Button",
                    {
                      style: {
                        marginRight: "5px"
                      },
                      props: {
                        type: params.row.status === "1" ? "error" : "primary",
                        size: "small"
                      }
                    },
                    params.row.status === "1" ? "停用" : "启用"
                  )
                ]
              ),
              h(
                "Poptip",
                {
                  props: {
                    confirm: true,
                    title: "您确定要删除这条规则吗?",
                    transfer: true,
                    placement: "top"
                  },
                  on: {
                    "on-ok": () => {
                      self.del(params.row.scheduledId);
                    }
                  }
                },
                [
                  h(
                    "Button",
                    {
                      props: { type: "error", size: "small" }
                    },
                    "删除"
                  )
                ]
              )
            ]);
          }
        }
      ],
      tableData: []
    };
  }, 
  methods: { 
    getData(res) {
      let self = this;
      // 初始化
      if (self.rules.length === 0) {
        self.rules = res.data;
        self.fetchListStart();
      }
    },
    getVal(expression) {
      let self = this;
      eval(expression);
      self.$forceUpdate();
    },
    fetchListStart() {
      let self = this;
      self.page = { ...self.pageModel };
      self.fetchList();
    },
    fetchList() {
      let self = this;
      self.loading = true;
	  self.autoHeight = 0;
      api
        .scheduledList(self.page)
        .then(data => {
          self.tableData = data || [];
          self.loading = false;
          // self.total = data.count || 0;
        })
        .catch(error => {
          self.loading = false;
        });
    },
    changePage(pageNum) {
      let self = this;
      self.page.pageNo = pageNum;
      self.fetchList();
    },
    reset() {
      let self = this;
      Object.keys(self.pageModel).forEach(x => {
        self.pageModel[x] = null;
      });
      self.fetchListStart();
    },
    // 启用/停用 任务
    updateStatus(scheduledId, status) {
      let self = this;
      self.loading = true;
      api
        .updateTaskStatus({ scheduledId, status })
        .then(data => {
          self.fetchListStart();
        })
        .catch(error => {
          self.loading = false;
        });
    },
    add() {
      this.scheduledModel = {};
      this.visible = true;
    },
    del(scheduledId) {
      let self = this;
      self.loading = true;
      api
        .scheduledDel([scheduledId])
        .then(data => {
          self.fetchListStart();
        })
        .catch(error => {
          self.loading = false;
        });
    }
  }
};
</script>

